package org.xmatch.framework.presentation;

import java.io.Serializable;
import java.util.List;

import javax.faces.model.DataModel;

public class GenericPagedDataModel extends DataModel implements Serializable {

	private static final long serialVersionUID = -1051998902962000534L;

	private int rowIndex = -1;

	private int rowCount;

	private int pageSize;

	@SuppressWarnings("unchecked")
	private List list;

	public GenericPagedDataModel() {
		super();
	}

	@SuppressWarnings("unchecked")
	public GenericPagedDataModel(List list, int rowCount) {
		super();
		setWrappedData(list);
		this.rowCount = rowCount;
		this.pageSize = list.size();
	}
	
	@SuppressWarnings("unchecked")
	public GenericPagedDataModel(List list, int rowCount, int pageSize) {
		super();
		setWrappedData(list);
		this.rowCount = rowCount;
		this.pageSize = pageSize;
	}

	@Override
	public boolean isRowAvailable() {
		if (list == null) {
			return false;
		}

		int rowIndex = getRowIndex();
		if (rowIndex >= 0 && rowIndex < list.size())
			return true;
		else
			return false;
	}

	@Override
	public int getRowCount() {
		return rowCount;
	}
	
	@Override
	public Object getRowData() {
		if (list == null)
			return null;
		else if (!isRowAvailable())
			throw new IllegalArgumentException();
		else {
			int dataIndex = getRowIndex();
			return list.get(dataIndex);
		}
	}

	@Override
	public int getRowIndex() {
		return (rowIndex % pageSize);
	}

	@Override
	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}

	@Override
	public Object getWrappedData() {
		return list;
	}

	@Override
	@SuppressWarnings("unchecked")
	public void setWrappedData(Object list) {
		this.list = (List) list;
	}

	public void ordenar(String sortField, boolean sortAscending) {
	
	}

}